package tools

import (
	"workflow-gui/internal/protocol"
)

// Registry manages available MCP tools
type Registry struct {
	tools map[string]protocol.Tool
}

// NewRegistry creates a new tool registry
func NewRegistry() *Registry {
	r := &Registry{
		tools: make(map[string]protocol.Tool),
	}
	r.registerTools()
	return r
}

// registerTools registers all available tools
func (r *Registry) registerTools() {
	// Register open_gui tool
	r.tools["open_gui"] = protocol.Tool{
		Name:        "open_gui",
		Description: "打开完整的 Workflow Manager GUI 主窗口。这是一个集成式界面，包含项目列表、工作流详情、AI 补充功能、对话式迭代等完整功能。",
		InputSchema: map[string]interface{}{
			"type":       "object",
			"properties": map[string]interface{}{},
			"required":   []string{},
		},
	}

	// Register create_workflow tool
	r.tools["create_workflow"] = protocol.Tool{
		Name:        "create_workflow",
		Description: "创建新的工作流。弹出原生 GUI 窗口让用户填写工作流名称和需求描述。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"summary": map[string]interface{}{
					"type":        "string",
					"description": "工作流名称（简短描述）",
				},
				"motivation": map[string]interface{}{
					"type":        "string",
					"description": "具体需求（详细描述为什么需要这个变更）",
				},
			},
			"required": []string{"summary", "motivation"},
		},
	}

	// Register ai_supplement_stage1 tool
	r.tools["ai_supplement_stage1"] = protocol.Tool{
		Name:        "ai_supplement_stage1",
		Description: "AI 自动补充 Stage 1 的 proposals 字段（priority, target_users, key_features 等）。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"confirm": map[string]interface{}{
					"type":        "boolean",
					"description": "是否直接确认（跳过用户查看），默认 false",
				},
			},
			"required": []string{"change_id"},
		},
	}

	// Register ai_supplement_stage2 tool
	r.tools["ai_supplement_stage2"] = protocol.Tool{
		Name:        "ai_supplement_stage2",
		Description: "AI 自动生成 Stage 2 的 requirements（基于 Stage 1 已确认的 proposals）。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"confirm": map[string]interface{}{
					"type":        "boolean",
					"description": "是否直接确认（跳过用户查看），默认 false",
				},
			},
			"required": []string{"change_id"},
		},
	}

	// Register ai_supplement_stage3 tool
	r.tools["ai_supplement_stage3"] = protocol.Tool{
		Name:        "ai_supplement_stage3",
		Description: "AI 自动生成 Stage 3 的 scenarios（基于 Stage 2 已确认的 requirements）。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"confirm": map[string]interface{}{
					"type":        "boolean",
					"description": "是否直接确认（跳过用户查看），默认 false",
				},
			},
			"required": []string{"change_id"},
		},
	}

	// Register ai_iterate_stage1 tool
	r.tools["ai_iterate_stage1"] = protocol.Tool{
		Name:        "ai_iterate_stage1",
		Description: "根据用户反馈迭代调整 Stage 1 的 proposals 字段。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"user_feedback": map[string]interface{}{
					"type":        "string",
					"description": "用户反馈内容（例如：\"priority 应该改成 P0\"）",
				},
			},
			"required": []string{"change_id", "user_feedback"},
		},
	}

	// Register ai_iterate_stage2 tool
	r.tools["ai_iterate_stage2"] = protocol.Tool{
		Name:        "ai_iterate_stage2",
		Description: "根据用户反馈迭代调整 Stage 2 的 requirements。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"user_feedback": map[string]interface{}{
					"type":        "string",
					"description": "用户反馈内容",
				},
			},
			"required": []string{"change_id", "user_feedback"},
		},
	}

	// Register ai_iterate_stage3 tool
	r.tools["ai_iterate_stage3"] = protocol.Tool{
		Name:        "ai_iterate_stage3",
		Description: "根据用户反馈迭代调整 Stage 3 的 scenarios。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"user_feedback": map[string]interface{}{
					"type":        "string",
					"description": "用户反馈内容",
				},
			},
			"required": []string{"change_id", "user_feedback"},
		},
	}

	// Register list_workflows tool
	r.tools["list_workflows"] = protocol.Tool{
		Name:        "list_workflows",
		Description: "列出所有工作流，支持按状态过滤和分页。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"status": map[string]interface{}{
					"type":        "string",
					"description": "状态过滤（可选）：draft | in_progress | completed",
					"enum":        []string{"draft", "in_progress", "completed"},
				},
				"page": map[string]interface{}{
					"type":        "number",
					"description": "页码（默认 1）",
					"default":     1,
				},
				"pageSize": map[string]interface{}{
					"type":        "number",
					"description": "每页数量（默认 10，最大 100）",
					"default":     10,
				},
			},
			"required": []string{},
		},
	}

	// Register get_workflow tool
	r.tools["get_workflow"] = protocol.Tool{
		Name:        "get_workflow",
		Description: "获取指定工作流的完整详情，包括 proposals、requirements、scenarios、tasks。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
			},
			"required": []string{"change_id"},
		},
	}

	// Register execute_task tool
	r.tools["execute_task"] = protocol.Tool{
		Name:        "execute_task",
		Description: "执行指定任务，调用 AI 生成实现代码，记录执行历史。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"task_id": map[string]interface{}{
					"type":        "number",
					"description": "任务 ID（例如：1, 2, 3）",
				},
			},
			"required": []string{"change_id", "task_id"},
		},
	}

	// Register save_stage1_results tool
	r.tools["save_stage1_results"] = protocol.Tool{
		Name:        "save_stage1_results",
		Description: "保存 Stage 1 AI 补充的结果（priority, target_users, key_features 等）到数据库。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"results": map[string]interface{}{
					"type":        "object",
					"description": "补充的字段结果（JSON 对象）",
					"properties": map[string]interface{}{
						"priority": map[string]interface{}{
							"type": "string",
							"enum": []string{"P0", "P1", "P2", "P3"},
						},
						"target_users": map[string]interface{}{
							"type": "string",
						},
						"key_features": map[string]interface{}{
							"type": "array",
							"items": map[string]interface{}{
								"type": "string",
							},
						},
						"affected_capabilities": map[string]interface{}{
							"type": "array",
							"items": map[string]interface{}{
								"type": "string",
							},
						},
						"breaking_changes": map[string]interface{}{
							"type": "boolean",
						},
					},
					"required": []string{"priority", "target_users", "key_features", "affected_capabilities", "breaking_changes"},
				},
			},
			"required": []string{"change_id", "results"},
		},
	}

	// Register save_stage2_results tool
	r.tools["save_stage2_results"] = protocol.Tool{
		Name:        "save_stage2_results",
		Description: "保存 Stage 2 AI 生成的 requirements 到数据库。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"requirements": map[string]interface{}{
					"type":        "array",
					"description": "需求列表",
					"items": map[string]interface{}{
						"type": "object",
						"properties": map[string]interface{}{
							"id": map[string]interface{}{
								"type": "string",
							},
							"operation": map[string]interface{}{
								"type": "string",
								"enum": []string{"ADDED", "MODIFIED", "REMOVED", "RENAMED"},
							},
							"name": map[string]interface{}{
								"type": "string",
							},
							"description": map[string]interface{}{
								"type": "string",
							},
						},
						"required": []string{"id", "operation", "name", "description"},
					},
				},
			},
			"required": []string{"change_id", "requirements"},
		},
	}

	// Register save_stage3_results tool
	r.tools["save_stage3_results"] = protocol.Tool{
		Name:        "save_stage3_results",
		Description: "保存 Stage 3 AI 生成的 scenarios 到数据库。",
		InputSchema: map[string]interface{}{
			"type": "object",
			"properties": map[string]interface{}{
				"change_id": map[string]interface{}{
					"type":        "string",
					"description": "工作流 ID（例如：WF-20251110-123456）",
				},
				"scenarios": map[string]interface{}{
					"type":        "array",
					"description": "场景列表",
					"items": map[string]interface{}{
						"type": "object",
						"properties": map[string]interface{}{
							"id": map[string]interface{}{
								"type": "string",
							},
							"name": map[string]interface{}{
								"type": "string",
							},
							"when": map[string]interface{}{
								"type": "string",
							},
							"then": map[string]interface{}{
								"type": "string",
							},
						},
						"required": []string{"id", "name", "when", "then"},
					},
				},
			},
			"required": []string{"change_id", "scenarios"},
		},
	}
}

// GetTools returns all registered tools
func (r *Registry) GetTools() []protocol.Tool {
	tools := make([]protocol.Tool, 0, len(r.tools))
	for _, tool := range r.tools {
		tools = append(tools, tool)
	}
	return tools
}

// GetTool returns a specific tool by name
func (r *Registry) GetTool(name string) (protocol.Tool, bool) {
	tool, ok := r.tools[name]
	return tool, ok
}
